package net.cruciblesoftware.ghostspacekillah;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import java.util.LinkedList;

class PlayingField {
    private PlayerShip player;
    private LinkedList<EnemyShip> enemies;
    private LinkedList<Bullet> bullets;
    private int enemyCount;

    private static PlayingField instance;

    public static PlayingField getInstance() {
        if(instance == null)
            instance = new PlayingField();
        return instance;
    }


    private PlayingField() {
        logi("creating PlayingField...");
        enemyCount = Settings.getInt("enemy_starting_count");
        player = new PlayerShip();
        enemies = new LinkedList<EnemyShip>();
        for(int i = 0; i < enemyCount; i++)
            enemies.add(new EnemyShip());
        bullets = new LinkedList<Bullet>();
    }


    public void update(float elapsed) {
        /* STEP ONE */
        /* Update all the FieldActor positions
         */
        player.update(elapsed);
        for(EnemyShip e : enemies)
            e.update(elapsed);
        for(Bullet b : bullets)
            b.update(elapsed);

        /* STEP TWO */
        /* Check for collisions and update states
         */
        for(EnemyShip e : enemies) {
            for(Bullet b : bullets) {
                if(e.detectCollision(b)) {
                    e.setState(Ship.STATE_EXPLODING);
                    b.setState(Bullet.STATE_INACTIVE);
                }
            }
            if(player.detectCollision(e)) {
                e.setState(Ship.STATE_EXPLODING);
                player.setState(Ship.STATE_EXPLODING);
            }
        }

        /* STEP THREE */
        /* Clean out the dead enemy ships
         */
        boolean checkAgain = true;
        while(checkAgain) {
            checkAgain = false;
            for(EnemyShip e : enemies) {
                if(e.getState() == Ship.STATE_DEAD) {
                    boolean ret = enemies.remove(e);
                    checkAgain = true;
                    break;
                }
            }
        }
        if(enemies.isEmpty()) {
            enemyCount *= 2;
            for(int i = 0; i < enemyCount; i++)
                enemies.add(new EnemyShip());
            player.shotDelayMultiplier(0.5f);
        }

        /* STEP FOUR */
        /* Clean out the drifting bullets
         */
        checkAgain = true;
        while(checkAgain) {
            checkAgain = false;
            for(Bullet b : bullets) {
                if(b.getState() == Bullet.STATE_INACTIVE) {
                    boolean ret = bullets.remove(b);
                    checkAgain = true;
                    break;
                }
            }
        }
    }


    public void draw(Canvas c) {
        player.draw(c);
        for(EnemyShip e : enemies) {
            e.draw(c);
        }
        for(Bullet b : bullets) {
            b.draw(c);
        }
    }


    public void addBullet(float posX, float posY, int rot) {
        bullets.add(new Bullet(posX, posY, rot));
    }


    /* Logging functions */
    private void logi(String msg) {
        android.util.Log.i(this.getClass().getName(), msg);
    }
    private void logw(String msg) {
        android.util.Log.w(this.getClass().getName(), msg);
    }
    private void loge(String msg) {
        android.util.Log.e(this.getClass().getName(), msg);
    }
}
